/*
 * COPYRIGHT:	2010 Christopher Smith, licenced under the GNU GPL v2; see COPYING in the top level directory
 * PROJECT:		sdedit
 * FILE:		editenter.cpp
 * PURPOSE:		a subclassed version of the EDIT control which eats and reports use of the enter key to the parent window
 *				
 */

namespace EditEnter
{

static USHORT const EEN_ENTER = 0xF000;

class WindowObject : public SubClassedWindowBase
{
	auto_szpod<WCHAR> EnteredText;

public:
	WindowObject(HWND Wnd)
	{
		WindowBase::Create(Wnd);

		EnteredText = GetNewWindowText(Wnd);
	}

	PCWSTR GetEnteredText()
	{
		return EnteredText;
	}

	virtual LRESULT RecieveMessage(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam) override
	{
		switch(msg)
		{
		case WM_KEYDOWN:
			switch(wparam)
			{
			case VK_RETURN:
				EnteredText = GetNewWindowText(Wnd);
				SendMessage(GetParent(Wnd), WM_COMMAND, MAKEWPARAM(GetWindowLong(Wnd, GWL_ID), EEN_ENTER), c_cast<LPARAM>(Wnd));
				return 0;
			}
			break;

		case WM_SETTEXT:
			{
				PCWSTR NewText = reinterpret_cast<PCWSTR>(lparam);
				EnteredText = NewText;

				break; //pass along to edit ctrl
			}

		case WM_KILLFOCUS:
			{
				__super::RecieveMessage(Wnd, WM_SETTEXT, NULL, reinterpret_cast<LPARAM>(EnteredText.get())); //cancelling, revert text control back
				break; //pass along to edit ctrl
			}

		case WM_GETDLGCODE:
			{
				MSG *mesg = reinterpret_cast<MSG *>(lparam);

				//want keydown return messages
				if(mesg && mesg->message == WM_KEYDOWN && mesg->wParam == VK_RETURN)
					return DLGC_WANTMESSAGE;
				else
					return __super::RecieveMessage(wnd, msg, wparam, lparam) | DLGC_UNDEFPUSHBUTTON; //steal black 'default/enter action' border from default button like command buttons normally do while focused
			}
			break;
		}

		return __super::RecieveMessage(wnd, msg, wparam, lparam);
	}
};

}//EditEnter